/*set working directory*/
/*ssc install blindschemes*/

/*Robustness for Figure 4.6*/
/*Data Set-Up*/
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
drop dup
generate org=1
by field fisyr, sort: egen density=sum(org)
label variable density "number of active INGOs"
keep field fisyr density
collapse (mean) density, by(field fisyr)
rename fisyr year
sort year field
merge year field using "Data/yearlist_1990-2019.dta"
replace density=0 if _merge==2
replace year=year+1 /*to lag density*/
drop if year>2019
drop _merge
sort field year
tempfile density
save `density'

use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
sort field fisyr totrev
by field fisyr: generate top_4 = _n > (_N - 4)
collapse (sum) totrev if top_4==1, by(field fisyr)
rename totrev top4rev
sort field fisyr
tempfile top4
save `top4'

use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
sort field fisyr totrev
collapse (sum) totrev, by(field fisyr)
bysort field: gen lag_totrev = totrev[_n-1]
generate pct_change = (totrev - lag_totrev) / lag_totrev * 100
generate abs_pct_change = abs(pct_change)
sort field fisyr
merge 1:1 field fisyr using `top4'
drop _merge
generate concentration4firm=top4rev/totrev
rename fisyr year
replace year=year+1 /*to lag concentration*/
sort field year
tempfile concentration
save `concentration'

use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
egen rank = rank(totrev), by(field fisyr)
egen count = count(totrev), by(field fisyr)
gen pct = 100 * (rank - 0.5) / count
gen percentile99=0
replace percentile99=1 if rank==count
replace percentile99=1 if rank!=count & pct>=98.5
collapse (sum) totrev if percentile99==1, by(field fisyr)
rename totrev rev99percentile
rename fisyr year
replace year=year+1 /*to lag alternative measure of concentration*/
sort field year
tempfile 99percentile
save `99percentile'

use "Data/missions_data.dta", clear
drop if foundingyear>2012
rename foundingyear year
generate specialist=.
replace specialist=1 if progscopenumeric==1 | geoscopenumeric==1 | geoscopenumeric==2
replace specialist=0 if progscopenumeric==2 & geoscopenumeric==3
rename field fieldalt
generate field=.
replace field=2 if fieldalt=="Relief"
replace field=3 if fieldalt=="Democracy"
replace field=4 if fieldalt=="Conservation"
replace field=5 if fieldalt=="Health"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
merge m:1 field year using `density'
drop _merge
sort field year
merge m:1 field year using `concentration'
drop _merge
sort field year
merge m:1 field year using `99percentile'

generate top1percentile=rev99percentile/totrev

replace revenue=0 if revenue<0
generate logrevenue=log(revenue+1)
label variable logrevenue "INGO Revenue"

generate dc=0
replace dc=1 if state=="DC"
label variable dc "DC Headquarters"

label variable faithbased "Faith-based INGO"
label variable concentration4firm "Concentration"

egen stddensity=std(density)
label variable stddensity "Density"

generate Sept11=0
replace Sept11=1 if year==2002 | year==2003 | year==2004
label variable Sept11 "Post-9/11"

egen volatility=std(abs_pct_change)
label variable volatility "Resource Volatility"

/*Table with regression results*/
reg specialist logrevenue dc faithbased if year>1992, vce(cluster field)
outreg2 using "Tables/figure4-6.tex", ctitle(Baseline Model) bdec(2) tdec(2) rdec(2) alpha(.01, .05, .1) replace
reg specialist logrevenue dc faithbased stddensity concentration4firm Sept11 volatility if year>1992, vce(cluster field)
outreg2 using "Tables/figure4-6.tex", ctitle(Full Model) bdec(2) tdec(2) rdec(2) alpha(.01, .05, .1) append

/*2 and 4 years following september 11*/
generate Sept11alt1=0
replace Sept11alt1=1 if year==2002 | year==2003 
label variable Sept11alt1 "Post-9/11"

generate Sept11alt2=0
replace Sept11alt2=1 if year==2002 | year==2003 | year==2004 | year==2005
label variable Sept11alt2 "Post-9/11"

reg specialist logrevenue dc faithbased if year>1992, vce(cluster field)
estimates store Baseline
reg specialist logrevenue dc faithbased stddensity concentration4firm Sept11alt1 volatility if year>1992, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure4-6_sept11alt1.pdf", as(pdf) replace

reg specialist logrevenue dc faithbased if year>1992, vce(cluster field)
estimates store Baseline
reg specialist logrevenue dc faithbased stddensity concentration4firm Sept11alt2 volatility if year>1992, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure4-6_sept11alt2.pdf", as(pdf) replace

/*Logit instead of OLS*/

logit specialist logrevenue dc faithbased if year>1992, vce(cluster field)
estimates store Baseline
reg specialist logrevenue dc faithbased stddensity concentration4firm Sept11 volatility if year>1992, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure4-6_logit.pdf", as(pdf) replace

/*Population Indicators*/

reg specialist logrevenue dc faithbased i.field if year>1992, vce(cluster field)
estimates store Baseline
reg specialist logrevenue dc faithbased stddensity concentration4firm Sept11 volatility i.field if year>1992, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure4-6_populationindicators.pdf", as(pdf) replace

/*Founding Year*/
reg specialist logrevenue dc faithbased if year>1992, vce(cluster field)
estimates store Baseline 
reg specialist logrevenue dc faithbased stddensity concentration4firm Sept11 volatility year if year>1992, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure4-6_age.pdf", as(pdf) replace

/*2, 4, and 6 year lags for density*/
use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
drop dup
generate org=1
by field fisyr, sort: egen density=sum(org)
label variable density "number of active INGOs"
keep field fisyr density
collapse (mean) density, by(field fisyr)
rename fisyr year
sort year field
merge year field using "Data/yearlist_1990-2019.dta"
replace density=0 if _merge==2
replace year=year+2 /*to lag density*/
drop if year>2019
drop _merge
rename density density_2yearslag
sort field year
tempfile density_2yearslag
save `density_2yearslag'

use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
drop dup
generate org=1
by field fisyr, sort: egen density=sum(org)
label variable density "number of active INGOs"
keep field fisyr density
collapse (mean) density, by(field fisyr)
rename fisyr year
sort year field
merge year field using "Data/yearlist_1990-2019.dta"
replace density=0 if _merge==2
replace year=year+4 /*to lag density*/
drop if year>2019
drop _merge
rename density density_4yearslag
sort field year
tempfile density_4yearslag
save `density_4yearslag'

use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
drop dup
generate org=1
by field fisyr, sort: egen density=sum(org)
label variable density "number of active INGOs"
keep field fisyr density
collapse (mean) density, by(field fisyr)
rename fisyr year
sort year field
merge year field using "Data/yearlist_1990-2019.dta"
replace density=0 if _merge==2
replace year=year+6 /*to lag density*/
drop if year>2019
drop _merge
rename density density_6yearslag
sort field year
tempfile density_6yearslag
save `density_6yearslag'

use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
sort field fisyr totrev
by field fisyr: generate top_4 = _n > (_N - 4)
collapse (sum) totrev if top_4==1, by(field fisyr)
rename totrev top4rev
sort field fisyr
tempfile top4
save `top4'

use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
sort field fisyr totrev
collapse (sum) totrev, by(field fisyr)
bysort field: gen lag_totrev = totrev[_n-1]
generate pct_change = (totrev - lag_totrev) / lag_totrev * 100
generate abs_pct_change = abs(pct_change)
sort field fisyr
merge 1:1 field fisyr using `top4'
drop _merge
generate concentration4firm=top4rev/totrev
rename fisyr year
replace year=year+1 /*to lag concentration*/
sort field year
tempfile concentration
save `concentration'

use "Data/PZ-1990-2019-Q.dta", clear
generate field=.
replace field=2 if nteefinal=="Q33"
replace field=3 if nteefinal=="Q35"
replace field=4 if nteefinal=="Q38"
replace field=5 if nteefinal=="Q39"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
drop if field==. 
sort ein fisyr
quietly by ein fisyr: gen dup = cond(_N==1, 0, _n)
sum dup
drop if dup>1 & ein!=.
egen rank = rank(totrev), by(field fisyr)
egen count = count(totrev), by(field fisyr)
gen pct = 100 * (rank - 0.5) / count
gen percentile99=0
replace percentile99=1 if rank==count
replace percentile99=1 if rank!=count & pct>=98.5
collapse (sum) totrev if percentile99==1, by(field fisyr)
rename totrev rev99percentile
rename fisyr year
replace year=year+1 /*to lag alternative measure of concentration*/
sort field year
tempfile 99percentile
save `99percentile'

use "Data/missions_data.dta", clear
drop if foundingyear>2012
rename foundingyear year
generate specialist=.
replace specialist=1 if progscopenumeric==1 | geoscopenumeric==1 | geoscopenumeric==2
replace specialist=0 if progscopenumeric==2 & geoscopenumeric==3
rename field fieldalt
generate field=.
replace field=2 if fieldalt=="Relief"
replace field=3 if fieldalt=="Democracy"
replace field=4 if fieldalt=="Conservation"
replace field=5 if fieldalt=="Health"
label define fieldgroups 2 "Relief" 3 "Democracy" 4 "Environment" 5 "Health"
label values field fieldgroups
sort field year
merge m:1 field year using `density_2yearslag'
drop _merge
sort field year
merge m:1 field year using `density_4yearslag'
drop _merge
sort field year
merge m:1 field year using `density_6yearslag'
drop _merge
sort field year
merge m:1 field year using `concentration'
drop _merge
sort field year
merge m:1 field year using `99percentile'

generate top1percentile=rev99percentile/totrev

replace revenue=0 if revenue<0
generate logrevenue=log(revenue+1)
label variable logrevenue "INGO Revenue"

generate dc=0
replace dc=1 if state=="DC"
label variable dc "DC Headquarters"

label variable faithbased "Faith-based INGO"
label variable concentration4firm "Concentration"

egen stddensity_2yearslag=std(density_2yearslag)
label variable stddensity_2yearslag "Density"

egen stddensity_4yearslag=std(density_4yearslag)
label variable stddensity_4yearslag "Density"

egen stddensity_6yearslag=std(density_6yearslag)
label variable stddensity_6yearslag "Density"

generate Sept11=0
replace Sept11=1 if year==2002 | year==2003 | year==2004
label variable Sept11 "Post-9/11"

egen volatility=std(abs_pct_change)
label variable volatility "Resource Volatility"

reg specialist logrevenue dc faithbased if year>1992, vce(cluster field)
estimates store Baseline
reg specialist logrevenue dc faithbased stddensity_2yearslag concentration4firm Sept11 volatility if year>1992, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure4-6_2yearlag.pdf", as(pdf) replace

reg specialist logrevenue dc faithbased if year>1992, vce(cluster field)
estimates store Baseline
reg specialist logrevenue dc faithbased stddensity_4yearslag concentration4firm Sept11 volatility if year>1992, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure4-6_4yearlag.pdf", as(pdf) replace

reg specialist logrevenue dc faithbased if year>1992, vce(cluster field)
estimates store Baseline
reg specialist logrevenue dc faithbased stddensity_6yearslag concentration4firm Sept11 volatility if year>1992, vce(cluster field)
estimates store Full
coefplot Baseline Full, drop(_cons) xline(0) ///
	p1(label(Baseline)) p2(label(Full))  ///
	ylabel(, labsize(medlarge)) xlabel(, labsize(medlarge)) ///
	legend(size(medlarge)) scheme(plotplain) scale(1.25) lwidth(thick)
graph export "Figures/Appendix/Figure4-6_6yearlag.pdf", as(pdf) replace
